package cmd

import (
	"axure_client/internal/logic"
	"context"
	"fmt"
	"path/filepath"
	"strings"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gcmd"
	"github.com/gogf/gf/v2/os/gfile"
	"github.com/gogf/gf/v2/os/gtime"
	"github.com/gogf/gf/v2/text/gstr"
	"github.com/gogf/gf/v2/util/gconv"
)

var (
	sourceDirKey  = "source"
	targetDirKey  = "target"
	remotePathKey = "remotePath"
	usernameKey   = "username"
	passwordKey   = "password"
	hostKey       = "host"
	portKey       = "port"
	uncompressKey = "uncompress"

	Main = gcmd.Command{
		Name:        "main",
		Usage:       "main",
		Brief:       "欢迎使用axure client",
		Description: "欢迎使用axure client",
	}

	Axure = gcmd.Command{
		Name:  "axure",
		Usage: "axure",
		Brief: `axure上传服务, 例如：./axure_client axure --username=xxx --password=xxxxxx --host=127.0.0.1 --port=22 --source=./rp --remotePath=/home/env/nginx/www/
		--------------------------------------------------
		--source:axure打包的文件夹 绝对路径或者相对路径都可以
		--target:压缩包名称，可不填（必须是.zip结尾））
		--remotePath:是远程服务器的路径，需要提前创建好  必传
		--host:远程服务器IP 必传
		--port:端口 必传
		--username:远程服务器用户名 必传
		--password:远程服务器密码 必传
		--uncompress:是否解压 默认true
		`,
		Description: "",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			g.Log().Info(ctx, "参数：", parser.GetOptAll())
			// 源文件夹路径
			sourceDirPath := parser.GetOpt(sourceDirKey).String()
			if len(sourceDirPath) == 0 {
				fmt.Println("请输入source源文件")
				return nil
			}
			zipName := gstr.SubStrFromEx(sourceDirPath, string(filepath.Separator))
			if len(zipName) == 0 {
				zipName = gtime.TimestampStr()
			}
			// 压缩目标zip文件路径
			targetZipPath := parser.GetOpt(targetDirKey, zipName+".zip").String()
			if len(targetZipPath) == 0 {
				fmt.Println("请输入target源文件")
				return nil
			}
			remotePath := parser.GetOpt(remotePathKey).String()
			if len(remotePath) == 0 {
				fmt.Println("请输入remotePath源文件")
				return nil
			}
			// remotePath必须以/结尾
			if !strings.HasSuffix(remotePath, string(filepath.Separator)) {
				remotePath = remotePath + "/"
			}
			uncompressStr := parser.GetOpt(uncompressKey).String()
			uncompress := true
			if len(uncompressStr) == 0 {
				uncompress = true
			} else {
				uncompress = gconv.Bool(uncompressStr)
			}
			// 获取文件名
			var fileName = gfile.Basename(targetZipPath)
			fmt.Println("文件名称:", fileName)
			// 调用ZipDirectory函数进行打包
			e := logic.ZipDirectory(sourceDirPath, targetZipPath)
			if e != nil {
				fmt.Println("打包过程中出现错误:", err)
			} else {
				fmt.Println("文件夹打包成功，压缩文件为:", targetZipPath)
			}
			// 上传文件
			username := parser.GetOpt(usernameKey).String()
			if len(username) == 0 {
				fmt.Println("请输入username")
				return nil
			}
			password := parser.GetOpt(passwordKey).String()
			if len(password) == 0 {
				fmt.Println("请输入password")
				return nil
			}
			host := parser.GetOpt(hostKey).String()
			if len(username) == 0 {
				fmt.Println("请输入username")
				return nil
			}
			port := parser.GetOpt(portKey).Int()
			if len(username) == 0 {
				fmt.Println("请输入port")
				return nil
			}
			config := logic.SSHConfig{
				Host:     host,
				Port:     port,
				User:     username,
				Password: password,
			}
			errorFile := logic.UploadFile(config, targetZipPath, remotePath+fileName, uncompress)
			if errorFile != nil {
				fmt.Println("上传异常", errorFile)
			}
			return nil
		},
	}
)
